import {
  classicBeh
} from "../classic-beh"
const mMgr = wx.getBackgroundAudioManager();

Component({
  /**
   * 组件的属性列表
   */
  behaviors: [classicBeh],
  properties: {
    src: String,
    title: String
  },


  /**
   * 组件的初始数据
   */
  data: {
    pauseSrc: 'images/player@waitting.png',
    playSrc: 'images/player@playing.png',
    playing: false
  },
  attached(event) {
    this.__recoverPlaying();
    this._monitroSwitch();
  },
  detached() {
    // mMGR.stop();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onPlay(event) {

      //  this.data.isplay !=this.data.isplay
      if (!this.data.playing) {
        this.setData({
          playing: true
        })
        mMgr.title = this.properties.title
        mMgr.src = this.properties.src
      } else {
        this.setData({
          playing: false
        })
        mMgr.pause();
      }

    },
    __recoverPlaying: function () {
      if (mMgr.paused) {
        this.setData({
          playing: false
        })
        return
      }
      if (mMgr.src == this.properties.src) {
        if (!mMgr.paused) {
          this.setData({
            playing: true
          })
        }
      }
    },
    _monitroSwitch() {
      mMgr.onPlay(() => {
        this.__recoverPlaying()
      })
      mMgr.onPause(() => {
        this.__recoverPlaying()
      })
      mMgr.onStop(() => {
          this.__recoverPlaying()
        }),
        mMgr.onEnded(() => {
          this.__recoverPlaying()
        })
    }
  }

})